package com.device.operating.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.device.operating.entity.Grouptab;
import com.device.operating.entity.User;
import com.device.operating.entity.Usergroup;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.DproleService;
import com.device.operating.service.GroupService;
import com.device.operating.service.RoleService;
import com.device.operating.service.UgService;
import com.device.operating.service.UgfuncService;
import com.device.operating.service.UgfunctypeService;
import com.device.operating.service.UserService;
import com.device.operating.utils.KeyProvider;

@Controller
public class GroupController extends BaseController{

	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="DepartmentService")
	private DepartmentService DepartmentService;
	
	@Resource(name="RoleService")
	private RoleService RoleService;
	
	@Resource(name="DproleService")
	private DproleService DproleService;
	
	@Resource(name="GroupService")
	private GroupService GroupService;
	
	@Resource(name="UgService")
	private UgService UgService;
	
	@Resource(name="UgfuncService")
	private UgfuncService UgfuncService;
	
	@Resource(name="UgfunctypeService")
	private UgfunctypeService UgfunctypeService;
	
	@RequestMapping("/togroup")
	public String togroup(HttpServletRequest request,ModelMap map){
		List<Grouptab> lg=GroupService.queryall();
		map.put("lg", lg);
		return "org/group.jsp";
	}
	
	@RequestMapping("/queryallgroup")
	@ResponseBody
	public Map queryallgroup(HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer rows){
		int fr=page-1;
		int mr=rows;
		Map map=new HashMap<>();
		List<Grouptab> lg=GroupService.queryall();
		int total=lg.size();
		int max=fr*mr+mr;
		if(total<max)
		max=total;
		if(total!=0)
		lg=lg.subList(fr*mr, max);
		map.put("rows", lg);
		map.put("total", total);				
		return map;
	}
	
	@RequestMapping("/toaddnewgroup")
	public String toaddnewgroup(ModelMap map){
		return "org/addgroup.jsp";
	}
	
	@RequestMapping(value="/addnewgroup",method=RequestMethod.POST)
	@ResponseBody
	public Map addnewgroup(HttpServletRequest request){
		Grouptab gp=new Grouptab();
		String gpname=request.getParameter("gpname");
		Map map=new HashMap<>();
		List<Grouptab> lg=GroupService.querybygpname(gpname);
		if(lg.size()!=0){
			map.put("msg", "名字重复!");
			return map;
		}
		String gpdescribe=request.getParameter("gpdescribe");
		gp.setGpdescribe(gpdescribe);
		gp.setGpname(gpname);
		gp.setGpId(KeyProvider.getPrimaryKey());
		GroupService.addgroup(gp);
		map.put("msg", "添加成功!");
		return map;
	}
	
	@RequestMapping("/getgroupuserlist")
	@ResponseBody
	public Map getgroupuserlist(HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer rows){
		String gpId=request.getParameter("gpId");
		if(gpId==null||gpId.equals("")){
			return null;
		}
		Map map=UgService.querybygpIdall(gpId,page-1, rows);
		return map;
	}
	
	@RequestMapping(value="/deleteuserfromgroup",method=RequestMethod.POST)
	@ResponseBody
	public Map deleteuserfromgroup(HttpServletRequest request){
		Map map=new HashMap<>();
		String ugId=request.getParameter("ugIds");
		String[] ugIds=ugId.split(",");
		for(int i=0;i<ugIds.length;i++){
			UgService.deleteug(ugIds[i]);
		}
		map.put("msg", "删除成功");
		return map;
	}
	
	@RequestMapping("/toaddusertogroup")
	public String toaddusertogroup(ModelMap map){
		List<Grouptab> lg=GroupService.queryall();
		map.put("lg", lg);
		return "org/addusertogroup.jsp";
	}
	
	@RequestMapping("/getgusers")
	@ResponseBody
	public List<Usergroup> getgusers(HttpServletRequest request){
		String gpId=request.getParameter("gpId");
		List<Usergroup> list=UgService.querybygpId(gpId);
		return list;
	}
	
	@RequestMapping("/addusertogroup")
	@ResponseBody
	public Map addusettogroup(HttpServletRequest request){
		Map map=new HashMap<>();
		String gpId=request.getParameter("groupId");
		String newuserIds=request.getParameter("newuserIds");
		String deleteuserIds=request.getParameter("deleteuserIds");
		Grouptab gp=GroupService.queryone(gpId);
		if(!newuserIds.equals("")){
			String[] newuserId=newuserIds.split(",");
			for(int i=0;i<newuserId.length;i++){
				User user=userService.queryUser(newuserId[i]);
				Usergroup ug=new Usergroup();
				ug.setGpId(gpId);
				ug.setGpname(gp.getGpname());
				ug.setUgId(KeyProvider.getPrimaryKey());
				ug.setUserId(newuserId[i]);
				ug.setUsername(user.getUsername());
				ug.setUsertel(user.getTel());
				ug.setEmail(user.getEmail());
				ug.setDpname(user.getDpname());
				ug.setRootdpId(user.getRootdpId());
				ug.setRootdpname(user.getRootdpname());
				UgService.addug(ug);
			}
		}
		if(!deleteuserIds.equals("")){
			String[] deleteuserId=deleteuserIds.split(",");
			for(int i=0;i<deleteuserId.length;i++){
				Usergroup ug=UgService.querybyuserIdandgpId(deleteuserId[i], gpId);
				UgService.deleteug(ug.getUgId());
			}
		}
		map.put("msg", "修改成功!");
		return map;
	}
	
	@RequestMapping("/togpfunc")
	public String togpfunc(ModelMap map){
		return "org/gpfunc.jsp";
	}
	
	@RequestMapping("/deletegroup")
	@ResponseBody
	public Map deletegroup(HttpServletRequest request){
		Map map=new HashMap<>();
		String gpId=request.getParameter("gpId");
		List<Usergroup> listug=UgService.querybygpId(gpId);
		if(listug.size()!=0){
			map.put("msg", "请先将人员移除分组！");
			return map;
		}
		UgfunctypeService.deleteugfunctypebyugId(gpId);
		UgfuncService.deleteugfuncbyugId(gpId);
		GroupService.deletegroup(gpId);
		map.put("msg", "删除成功！");
		return map;
	}
}
